package com.automotive.can1939.jni;

import com.automotive.can1939.model.CanFrame;

/**
 * CAN总线JNI接口
 * 提供与底层CAN驱动的通信接口
 */
public class CanNativeInterface {
    private static final String TAG = "CanNativeInterface";
    
    // 加载本地库
    static {
        try {
            System.loadLibrary("can_native");
        } catch (UnsatisfiedLinkError e) {
            android.util.Log.e(TAG, "Failed to load native library: " + e.getMessage());
        }
    }
    
    // 回调接口，用于从Native层回调Java层
    public interface CanDataCallback {
        /**
         * 接收到CAN数据时的回调
         * @param canId CAN ID
         * @param data 数据字节数组
         * @param dataLength 数据长度
         * @param isExtended 是否为扩展帧
         * @param timestamp 时间戳
         */
        void onCanDataReceived(int canId, byte[] data, int dataLength, boolean isExtended, long timestamp);
    }
    
    public interface CanStatusCallback {
        /**
         * CAN总线状态变化回调
         * @param status 状态码 (0: 断开, 1: 连接, 2: 错误)
         * @param message 状态消息
         */
        void onCanStatusChanged(int status, String message);
    }
    
    private static CanDataCallback dataCallback;
    private static CanStatusCallback statusCallback;
    
    /**
     * 设置数据回调接口
     */
    public static void setDataCallback(CanDataCallback callback) {
        CanNativeInterface.dataCallback = callback;
    }
    
    /**
     * 设置状态回调接口
     */
    public static void setStatusCallback(CanStatusCallback callback) {
        CanNativeInterface.statusCallback = callback;
    }
    
    /**
     * 初始化CAN接口
     * @param devicePath CAN设备路径 (如 "/dev/can0")
     * @param baudRate 波特率
     * @return 0: 成功, 其他: 错误码
     */
    public static native int initCan(String devicePath, int baudRate);
    
    /**
     * 连接CAN总线
     * @return 0: 成功, 其他: 错误码
     */
    public static native int connectCan();
    
    /**
     * 断开CAN总线连接
     * @return 0: 成功, 其他: 错误码
     */
    public static native int disconnectCan();
    
    /**
     * 发送CAN帧
     * @param canId CAN ID
     * @param data 数据字节数组
     * @param dataLength 数据长度
     * @param isExtended 是否为扩展帧
     * @return 0: 成功, 其他: 错误码
     */
    public static native int sendCanFrame(int canId, byte[] data, int dataLength, boolean isExtended);
    
    /**
     * 启动CAN数据接收
     * @return 0: 成功, 其他: 错误码
     */
    public static native int startReceiving();
    
    /**
     * 停止CAN数据接收
     * @return 0: 成功, 其他: 错误码
     */
    public static native int stopReceiving();
    
    /**
     * 设置CAN过滤器
     * @param filterId 过滤器ID
     * @param mask 过滤器掩码
     * @return 0: 成功, 其他: 错误码
     */
    public static native int setCanFilter(int filterId, int mask);
    
    /**
     * 获取CAN总线状态
     * @return 状态码 (0: 断开, 1: 连接, 2: 错误)
     */
    public static native int getCanStatus();
    
    /**
     * 释放CAN资源
     * @return 0: 成功, 其他: 错误码
     */
    public static native int releaseCan();
    
    // JNI回调方法，由Native层调用
    @SuppressWarnings("unused")
    public static void onCanDataReceivedFromNative(int canId, byte[] data, int dataLength, 
                                                   boolean isExtended, long timestamp) {
        if (dataCallback != null) {
            dataCallback.onCanDataReceived(canId, data, dataLength, isExtended, timestamp);
        }
    }
    
    @SuppressWarnings("unused")
    public static void onCanStatusChangedFromNative(int status, String message) {
        if (statusCallback != null) {
            statusCallback.onCanStatusChanged(status, message);
        }
    }
    
    /**
     * 错误码定义
     */
    public static class ErrorCode {
        public static final int SUCCESS = 0;
        public static final int ERROR_INIT_FAILED = -1;
        public static final int ERROR_CONNECT_FAILED = -2;
        public static final int ERROR_SEND_FAILED = -3;
        public static final int ERROR_RECEIVE_FAILED = -4;
        public static final int ERROR_INVALID_PARAM = -5;
        public static final int ERROR_DEVICE_NOT_FOUND = -6;
        public static final int ERROR_PERMISSION_DENIED = -7;
        public static final int ERROR_ALREADY_CONNECTED = -8;
        public static final int ERROR_NOT_CONNECTED = -9;
    }
    
    /**
     * 状态码定义
     */
    public static class StatusCode {
        public static final int DISCONNECTED = 0;
        public static final int CONNECTED = 1;
        public static final int ERROR = 2;
    }
}